package cc.lzmsh.service.impl;

import cc.lzmsh.mapper.UserMapper;
import cc.lzmsh.pojo.User;
import cc.lzmsh.pojo.dto.LoginDTO;
import cc.lzmsh.pojo.dto.RegisterDTO;
import cc.lzmsh.pojo.result.Result;
import cc.lzmsh.service.UserService;
import cc.lzmsh.utils.JwtUtil;
import cc.lzmsh.utils.Md5Util;
import cc.lzmsh.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Author:   msh
 * Date:     2024/9/25 下午2:55
 * Description:
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public Result register(RegisterDTO registerDTO) {
        if (!StringUtils.hasText(registerDTO.getUsername()) &&
                !StringUtils.hasText(registerDTO.getPassword())) {
            throw new RuntimeException("用户名和密码不能为空");
        }
        if (registerDTO.getUsername().length() < 3 || registerDTO.getUsername().length() > 16) {
            throw new RuntimeException("用户名长度为3-16位");
        }
        if (registerDTO.getPassword().length() < 6 || registerDTO.getPassword().length() > 16) {
            throw new RuntimeException("密码长度为6-16位");
        }
//        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
//            throw new RuntimeException("两次输入的密码不一致");
//        }
        if (userMapper.selectUserByUsername(registerDTO.getUsername()) != null) {
            throw new RuntimeException("用户名已存在");
        }
        User user = new User();
        BeanUtils.copyProperties(registerDTO, user);
        user.setPassword(Md5Util.getMD5String(user.getPassword()));
        userMapper.insertUser(user);
        return Result.success();
    }

    @Override
    public Result login(LoginDTO loginDTO) {
        User user = userMapper.selectUserByUsername(loginDTO.getUsername());
        if (user == null) {
            return Result.error("用户不存在");
        }
        if (!Md5Util.checkPassword(loginDTO.getPassword(), user.getPassword())) {
            return Result.error("密码错误");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", user.getId());
        map.put("username", user.getUsername());
        String token = JwtUtil.genToken(map);


        // 将token存入redis
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set(token, token);
        return Result.success(token);
    }

    @Override
    public User getUserInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public Result updateUserInfo(User user) {
        Map<String, Object> map = ThreadLocalUtil.get();
        user.setId((Integer) map.get("userId"));
        userMapper.updateUserInfo(user);
        return Result.success();
    }

    @Override
    public User findByUserName(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public Result updatePwd(String newPwd) {
        String md5String = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(md5String);
        return Result.success();
    }

}
